** estimate effects for each cognitive domain 

***Inputs: 
* $Data/GameUserWPdaily_norm.dta
* $Data/maxmindirectionsmonth_zip260.dta


***Outputs: 
* $Results/bydomain
* $Results/pval_het_domain.tex
* $Results/pvals_domain.dta
* $Results/bydomaininteraction.png
* $Results/bydomaininteraction_above5play.png
* $Results/playsgame.tex
* $Results/gameobs.png
* $Results/gameobs.tex

	
	use $Data/GameUserWPdaily_norm.dta, clear
	
	gen month = month(date)

		
	gen zip2 = int( zip/10)
	
	merge m:1 zip2 month using $Data/maxmindirectionsmonth_zip260.dta
	
	keep if _merge ==3
	
	
	gen monthyear = mofd(date)

	gen highpm =  weighted_dailypm > 25 & weighted_dailypm<. 

	
	
	egen cut_winddirection = cut(median_closestwind), at(0(60)360)
	replace cut_winddirection = 0 if median_closestwind ==360

	
	
	gen maxzipwind = (cut_winddirection ==maxdirection )
	replace maxzipwind = . if maxdirection==.
	
	keep if maxzipwind<. 
	
	
		egen cut_temp = cut(weighted_temperature), at(-40(10)40)

		
		keep norm_score highpm gamenum maxzipwind weighted_wind_speed weighted_precipitation anon_id monthyear cut_temp nth_play
	label variable highpm "Daily PM2.5 $>$ 25" 

	
	
	ivreghdfe norm_score (c.highpm#i.gamenum =c.maxzipwind#i.gamenum ) weighted_wind_speed weighted_precipitation, absorb(gamenum anon_id monthyear cut_temp) cluster(anon_id monthyear)

	eststo bydomaininteraction
	
	estimates save $Results/bydomain , replace
	
	* test difference between memory and other domains 
	
	test 55b.gamenum#c.highpm 77.gamenum#c.highpm
	matrix p55= r(p)
	
	test 74.gamenum#c.highpm 77.gamenum#c.highpm
	matrix p74= r(p)

	test 76.gamenum#c.highpm 77.gamenum#c.highpm
	matrix p76= r(p)

	test 78.gamenum#c.highpm 77.gamenum#c.highpm
	matrix p78= r(p)
	
	test 84.gamenum#c.highpm 77.gamenum#c.highpm
	matrix p84= r(p)
	
	test 104.gamenum#c.highpm 77.gamenum#c.highpm
	matrix p104= r(p)
	
	matrix domain_test =[p55 \  p74 \ p76 \ p78 \ p84 \ p104]
	
	matrix rownames domain_test  = "Verbal" "Attention" "Flexibility" "Math" "Speed" "Problem Solving"
	matrix colnames domain_test  = "p-value"
	
	esttab matrix(domain_test, fmt(3)) using $Results/pval_het_domain.tex, 
	
	regsave 55b.gamenum#c.highpm 74.gamenum#c.highpm 76.gamenum#c.highpm 77.gamenum#c.highpm 78.gamenum#c.highpm 84.gamenum#c.highpm 104.gamenum#c.highpm using $Results/pvals_domain.dta , pval
	
	
	
  coefplot (bydomaininteraction, keep(55.gamenum#c.highpm) ciopts(recast(rcap) lcolor(red%30))  mcolor(red) msymbol(D) msize(large)) ///
   (bydomaininteraction, keep(74.gamenum#c.highpm) ciopts(recast(rcap) lcolor(orange%30))  mcolor(orange) msymbol(D) msize(large)) ///
    (bydomaininteraction, keep(76.gamenum#c.highpm) ciopts(recast(rcap) lcolor(gold%30)) mcolor(gold) msymbol(D) msize(large)) ///
	 (bydomaininteraction, keep(77.gamenum#c.highpm) ciopts(recast(rcap) lcolor(green%30)) mcolor(green) msymbol(D) msize(large)) ///
	  (bydomaininteraction, keep(78.gamenum#c.highpm)  ciopts(recast(rcap) lcolor(blue%30)) mcolor(blue) msymbol(D) msize(large)) ///
	   (bydomaininteraction, keep(84.gamenum#c.highpm) ciopts(recast(rcap) lcolor("111 000 255 %30")) mcolor("111 000 255") msymbol(D) msize(large)) ///
	    (bydomaininteraction, keep(104.gamenum#c.highpm) ciopts(recast(rcap) lcolor("238 130 238 %30")) mcolor("238 130 238") msymbol(D) msize(large)),  ///
		 coeflabels(55.gamenum#c.highpm = "Verbal" ///
               74.gamenum#c.highpm = "Attention" ///
			  76.gamenum#c.highpm  = "Flexibility" ///
			   77.gamenum#c.highpm = "Memory" ///
			   78.gamenum#c.highpm = "Math" ///
			   84.gamenum#c.highpm = "Speed" ///
			   104.gamenum#c.highpm ="Problem Solving" 	 )    vertical levels(95) baselevels omitted  nooffsets  legend(off)  yline(0, lcolor(gs0%10))
  
			   
			   
			   			   graph export $Results/bydomaininteraction.png, replace
	

		   
		   
		   
		   
		   	gen above5play = nth_play >= 5 & nth_play<.

		   
		   
	
	ivreghdfe norm_score (c.highpm#i.gamenum =c.maxzipwind#i.gamenum ) weighted_wind_speed weighted_precipitation, absorb(gamenum anon_id monthyear cut_temp above5play) cluster(anon_id monthyear)

	eststo bydomaininteraction
	
	
	
  coefplot (bydomaininteraction, keep(55.gamenum#c.highpm) ciopts(recast(rcap) lcolor(red%30))  mcolor(red) msymbol(D) msize(large)) ///
   (bydomaininteraction, keep(74.gamenum#c.highpm) ciopts(recast(rcap) lcolor(orange%30))  mcolor(orange) msymbol(D) msize(large)) ///
    (bydomaininteraction, keep(76.gamenum#c.highpm) ciopts(recast(rcap) lcolor(gold%30)) mcolor(gold) msymbol(D) msize(large)) ///
	 (bydomaininteraction, keep(77.gamenum#c.highpm) ciopts(recast(rcap) lcolor(green%30)) mcolor(green) msymbol(D) msize(large)) ///
	  (bydomaininteraction, keep(78.gamenum#c.highpm)  ciopts(recast(rcap) lcolor(blue%30)) mcolor(blue) msymbol(D) msize(large)) ///
	   (bydomaininteraction, keep(84.gamenum#c.highpm) ciopts(recast(rcap) lcolor("111 000 255 %30")) mcolor("111 000 255") msymbol(D) msize(large)) ///
	    (bydomaininteraction, keep(104.gamenum#c.highpm) ciopts(recast(rcap) lcolor("238 130 238 %30")) mcolor("238 130 238") msymbol(D) msize(large)),  ///
		 coeflabels(55.gamenum#c.highpm = "Verbal" ///
               74.gamenum#c.highpm = "Attention" ///
			  76.gamenum#c.highpm  = "Flexibility" ///
			   77.gamenum#c.highpm = "Memory" ///
			   78.gamenum#c.highpm = "Math" ///
			   84.gamenum#c.highpm = "Speed" ///
			   104.gamenum#c.highpm ="Problem Solving" 	 )    vertical levels(95) baselevels omitted  nooffsets  legend(off)  yline(0, lcolor(gs0%10))
  
			   
			   
			   			   graph export $Results/bydomaininteraction_above5play.png, replace

		   
						   
						   
	******** Distribution of games
	
	
	
		gen ones = 1
		
		
		
	
	gen newgamenum = 1 if gamenum==55 
	replace newgamenum =2 if gamenum ==74
	replace newgamenum =3 if gamenum ==76
	replace newgamenum =4 if gamenum ==77
	replace newgamenum =5 if gamenum ==78
	replace newgamenum =6 if gamenum ==84
	replace newgamenum =7 if gamenum ==104
	
	
	label define games 1 "Verbal" 2 "Attention" 3 "Flexibility" 4 "Memory" 5 "Math" 6 "Speed" 7 "Problem Solving"
	label values newgamenum games
	
	
	
	
		reg nth_play i.newgamenum if maxzipwind<. 
		
		eststo playsgame
		
		
		
	esttab playsgame using $Results/playsgame.tex, ci(3) b(3)  replace label 
	
			  
			  
		collapse (sum) Obs = ones , by(newgamenum)
		
		label variable Obs "Observations (000s)"
	
	replace Obs = Obs/1000
	
	
	
	coefplot 	(playsgame, keep(1.newgamenum) ciopts(recast(rcap) lcolor(red)) msymbol(D) mcolor(red) msize(large) ) ///
	(playsgame, keep(2.newgamenum) ciopts(recast(rcap) lcolor(orange))  msymbol(D) mcolor(orange) msize(large)) ///
	(playsgame, keep(3.newgamenum) ciopts(recast(rcap) lcolor(gold)) msymbol(D) mcolor(gold) msize(large)) ///
	(playsgame, keep(4.newgamenum) ciopts(recast(rcap) lcolor(green)) msymbol(D) mcolor(green) msize(large)) ///
	(playsgame, keep(5.newgamenum) ciopts(recast(rcap) lcolor(blue)) msymbol(D) mcolor(blue) msize(large)) ///
	( playsgame, keep(6.newgamenum) ciopts(recast(rcap) lcolor("111 000 255")) mcolor("111 000 255") msymbol(D) msize(large) ) ///	
	( playsgame, keep(7.newgamenum) ciopts(recast(rcap) lcolor("238 130 238")) mcolor("238 130 238") msymbol(D) msize(large) ) ///
	, yaxis(2) ytitle(Difference in plays relative to Verbal, axis(2)) vertical levels(95) yline(0)  nooffsets  legend(off) xlabel(, angle(60))  msymbol(D) omitted baselevels xscale(range(1 7)) 
	
	
	
	
	
	addplot: bar Obs newgamenum if newgamenum==1, bcolor(red%20) yaxis(1) yscale(range(0 1000) axis(1)) ylabel(0(250)1000 ,axis(1))
	addplot: bar Obs newgamenum if newgamenum==2, bcolor(orange%20) yaxis(1) yscale(range(0 1000) axis(1))  ylabel(0(250)1000 ,axis(1))
	addplot: bar Obs newgamenum if newgamenum==3, bcolor(gold%20) yaxis(1) yscale(range(0 1000) axis(1))  ylabel(0(250)1000 ,axis(1))
	addplot: bar Obs newgamenum if newgamenum==4, bcolor(green%20) yaxis(1)  yscale(range(0 1000) axis(1))  ylabel(0(250)1000 ,axis(1))
	addplot: bar Obs newgamenum if newgamenum==5, bcolor(blue%20) yaxis(1) yscale(range(0 1000) axis(1))  ylabel(0(250)1000 ,axis(1))
	addplot: bar Obs newgamenum if newgamenum==6, bcolor("111 000 255 %20") yaxis(1) yscale(range(0 1000) axis(1))  ylabel(0(250)1000 ,axis(1))
	addplot: bar Obs newgamenum if newgamenum==7, bcolor("238 130 238 %20") yaxis(1) yscale(range(0 1000) axis(1)) ylabel(0(250)1000, axis(1)) ytitle(Observations (000s), axis(1))


	graph export $Results/gameobs.png, replace
						   
						   
						   
			
		estpost tabstat Obs , by(newgamenum) 	statistics(mean ) columns(statistics) listwise

 
		
		esttab . using $Results/gameobs.tex, main(mean) nostar unstack noobs nonote label mtitle("1000 Observations by Game") replace
		
